package cn.com.jcoo.satoken;

import cn.com.jcoo.ehcahce.SaTokenEhcacheManager;
import cn.dev33.satoken.dao.SaTokenDao;
import cn.dev33.satoken.util.SaFoxUtil;
import net.sf.ehcache.Cache;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@ConditionalOnBean(SaTokenEhcacheManager.class)
@SuppressWarnings("unused")
public class SaTokenEhcacheDao implements SaTokenDao {

    private final SaTokenEhcacheManager saTokenEhcacheManager;

    public SaTokenEhcacheDao(SaTokenEhcacheManager saTokenEhcacheManager) {
        this.saTokenEhcacheManager = saTokenEhcacheManager;
    }


    @Override
    public String get(String key) {
        return saTokenEhcacheManager.getStr(key);
    }

    @Override
    public void set(String key, String value, long timeout) {
        if (timeout > Integer.MAX_VALUE) {
            timeout = Integer.MAX_VALUE;
        }
        saTokenEhcacheManager.set(key, value, (int) timeout);
    }

    @Override
    public void update(String key, String value) {

        long expiration = saTokenEhcacheManager.getKeyExpiration(key);
        saTokenEhcacheManager.set(key, value, (int) expiration);
    }

    @Override
    public void delete(String key) {
        saTokenEhcacheManager.removeKey(key);
    }

    @Override
    public long getTimeout(String key) {
        return saTokenEhcacheManager.getKeyExpiration(key);
    }

    @Override
    public void updateTimeout(String key, long timeout) {
        if (timeout > Integer.MAX_VALUE) {
            timeout = Integer.MAX_VALUE;
        }
        String oldValue = saTokenEhcacheManager.getStr(key);
        saTokenEhcacheManager.set(key, oldValue, (int) timeout);
    }

    @Override
    public Object getObject(String key) {
        return saTokenEhcacheManager.get(key);
    }

    @Override
    public void setObject(String key, Object object, long timeout) {
        if (timeout > Integer.MAX_VALUE) {
            timeout = Integer.MAX_VALUE;
        }
        saTokenEhcacheManager.set(key, object, (int) timeout);
    }

    @Override
    public void updateObject(String key, Object object) {

        long expiration = saTokenEhcacheManager.getKeyExpiration(key);
        saTokenEhcacheManager.set(key, object, (int) expiration);
    }

    @Override
    public void deleteObject(String key) {
        saTokenEhcacheManager.removeKey(key);
    }

    @Override
    public long getObjectTimeout(String key) {
        return saTokenEhcacheManager.getKeyExpiration(key);
    }

    @Override
    public void updateObjectTimeout(String key, long timeout) {
        if (timeout > Integer.MAX_VALUE) {
            timeout = Integer.MAX_VALUE;
        }
        String oldValue = saTokenEhcacheManager.getStr(key);
        saTokenEhcacheManager.set(key, oldValue, (int) timeout);
    }

    @Override
    public List<String> searchData(String prefix, String keyword, int start, int size, boolean sortType) {
        Cache cache = saTokenEhcacheManager.getCache();
        List<String> keys = cache.getKeys();
        return SaFoxUtil.searchList(keys, prefix, keyword, start, size, sortType);
    }
}
